<?php

class fungsi {

    function __construct() {

    }
    function formatUang($angka,$mtUang = 'Rp.'){
        return $mtUang.number_format($angka,0,',','.');
    }
    function readMore($str,$length=20,$titik=3){
        $stRet ='';
        $stRet .= (strlen($str)>$length)?substr($str,0,($length-$titik)).str_repeat('.',$titik):$str;
        return $stRet;
    }
    function optAktifasi($stat){
        return $stat==0?"Aktifkan":"NonAktifkan";
    }
    
    function potongKalimat($strKalimat,$panjang=20,$mulaiDari=0){
        return strlen($strKalimat)>$panjang?substr($strKalimat,$mulaiDari,$panjang-3)."...":$strKalimat;
    }
    function buatJudul($judul,$jmlStrip=84){
        return '<span class="font_judul">'.$judul.'<br />
        <span class="font_line">'.str_repeat('-',$jmlStrip).'
        </span>
        </span>';
    }
    
    function tanggal1($strDate){
        return date('d F Y H:i:s',strtotime($strDate));
    }
    function tanggal2($strDate){
        return date('d F Y',strtotime($strDate));
    }
    function arrayErrorMsg($msg){
        return array('success'=>false,'error_msg'=>$msg);
    }
    function inisialisasiProses($on=null){
        if(isset($on) && strlen($on)>0){
            return $on;
        }
        else{
            $s['success']=false;
            $s['error_msg']='Direct Access Forbidden';
            echo json_encode($s);
            exit;
        }
    }
    function var_dump($data,$ex = true){
        echo('<pre>');
        var_dump($data);
        echo('<pre>');
        if($ex==true){
            exit;
        }
    }
    function alert($msg){
        return '<script type="text/javascript">alert("'.$msg.'");</script>';
    }
    function redirect($url,$time=0){
        return '<meta http-equiv="refresh" content="'.$time.';url='.$url.'">';
    }
    function unggah($namaInput,$direktori,$namaFileBaru=null){
        $extArr = explode('.', $_FILES[$namaInput]['name']);
        $ext = $extArr[count($extArr)-1];
        $namaFile = isset($namaFileBaru)?$namaFileBaru:time().'_'.  rand(1,9999).'.'.$ext;
        $mv = move_uploaded_file($_FILES[$namaInput]['tmp_name'], $direktori.'/'.$namaFile);
        if($mv==true){
            $arr = array('success'=>true,'filename'=>$namaFile);
        }
        else{
            $arr = array('success'=>false,'error_msg'=>$namaFile);
        }
        return $arr;
    }
    function hapusFile($linkFile){
        return unlink($linkFile);
    }
    function buatHalaman($totalData, $dataPerHalaman, $halamanKe, $strParams = '') {
        $strResult = '';
        $showPage = $halamanKe;
        $jmlPage = ceil($totalData / $dataPerHalaman);
        $noPage = ((int) $halamanKe > 0) ? $halamanKe : 0;
        $offset = ($noPage - 1) * $dataPerHalaman;
        if ($noPage > 0) {
            $strResult .="<a class='pagination' href='" . $_SERVER['PHP_SELF'] . "?hal=" . ($noPage - 1) . $strParams . "'>&lt;&lt; Sebelum</a>";
        };
        if ($noPage > 0) {
            $strResult .="<a class='pagination' href=" . $_SERVER['PHP_SELF'] . "?hal=0" . $strParams . ">Pertama</a>";
        } else {
            $strResult .= "<b class='pagination'>Pertama</b>";
        };
        for ($page = 1; $page < $jmlPage; $page++) {
            if ((($page >= $noPage - 3) && ($page <= $noPage + 3)) || ($page == 1) || ($page == $jmlPage)) {

                if ($page == $noPage) {
                    $hT = $page;
                    $strResult .= " <b class='pagination'>" . ($hT+1 ) . "</b> ";
                } else {
                    $hT = $page;
                    $strResult .= " <a class='pagination' href='" . $_SERVER['PHP_SELF'] . "?hal=" . $page . $strParams . "'>" . ($hT+1) . "</a> ";
                }
                $showPage = $page;
            }
        }
        if ($noPage < ($jmlPage - 1)) {
            $strResult .="<a class='pagination' href=" . $_SERVER['PHP_SELF'] . "?hal=" . ($jmlPage - 1) . $strParams . ">Terakhir</a>";
        } else {
            $strResult .= "<b class='pagination'>Terakhir</b>";
        };
        if (($noPage + 1) < $jmlPage) {
            $strResult .= "<a class='pagination' href='" . $_SERVER['PHP_SELF'] . "?hal=" . ($noPage + 1) . $strParams . "'>Berikut &gt;&gt;</a>";
        }
        if($totalData<=$dataPerHalaman){
            $strResult='';
        };
        //$strResult .="<br><a onclick='return false' class='pagination' href='#'>Total: ".$totalData."</a>";
        return $strResult;
    }

}
$objFungsi = new fungsi();